import focusTabs from '../../consts/focusTabs';
import { create } from '../../services/Editor/timelines';

export default {
    namespace: 'timelines',
    state: [],
    reducers: {
        addTimelinesOfPage(oldTimelines, { payload: pTimelines }) {
            const timelines = oldTimelines.concat(pTimelines);
            return timelines;
        },
        clear() {
            return [];
        },
        updateTimeline(oldTimelines, { payload: timeline }) {
            const sTimelines = oldTimelines.filter(w => w.timeline_id === timeline.timeline_id);
            if (sTimelines.length > 0) {
                const sIndex = oldTimelines.indexOf(sTimelines[0]);
                const tTimeline = { ...sTimelines[0], ...timeline };
                oldTimelines[sIndex] = tTimeline;
            }
            return oldTimelines;
        },
        updateTimelines(oldTimelines, { payload: timelines }) {
            // 记录所有需要修改的key值
            const timelineIds = {};
            for (let i = 0; i < timelines.length; i++) {
                timelineIds[timelines[i].timeline_id] = i;
            }
            // 修改
            for (let i = 0; i < oldTimelines.length; i++) {
                const tTimelineId = oldTimelines[i].timeline_id;
                if (timelineIds[tTimelineId] !== undefined) {
                    const index = timelineIds[tTimelineId];
                    oldTimelines[i] = { ...oldTimelines[i], ...timelines[index] };
                }
            }
            return oldTimelines;
        },
        removeTimeline(oldTimelines = [], { payload: timeline }) {
            const sTimelines = oldTimelines.filter(w => w.timeline_id === timeline.timeline_id);
            if (sTimelines.length > 0) {
                const index = oldTimelines.indexOf(sTimelines[0]);
                oldTimelines[index].delFlag = true;
                return [...oldTimelines];
            }
            return oldTimelines;
        },
        removeTimelines(oldTimelines = [], { payload: timelines }) {
            const timelinesIds = timelines.map(w => w.timeline_id);
            const newTimelines = oldTimelines.map((w) => {
                if (timelinesIds.indexOf(w.timeline_id) !== -1) {
                    w.delFlag = true;
                }
                return w;
            });
            return [...newTimelines];
        },
        removeTimelinesByWidgetsIds(oldTimelines = [], { payload: widgetsIds }) {
            const newTimelines = oldTimelines.map(w => {
                if(widgetsIds.indexOf(w.widget_id) !== -1) {
                    w.delFlag = true;
                }
                return w;
            });

            return [...newTimelines];
        },
        resetTimelines(oldTimelines = [], { payload: timelines }) {
            return [...timelines];
        },
    },
    effects: {
        /**
         * 选中某个动效
         */
        *selectTimeline({ payload: timeline }, { put }) {
            // 获取focusTab值
            const focusTab = focusTabs.ANIMATION;
            // 修改特定值
            yield put({
                type: 'editor/setState',
                payload: {
                    selectedWidgetIds: [],
                    selectedWidgetId: timeline.widget_id,
                    selectedWidgetTimelineId: timeline.timeline_id,
                    selectedTimelineId: timeline.timeline_id,
                    selectedTimeline: timeline,
                    focusTab,
                },
            });
        },
        /*
        * 添加动效
        * create不会返回widgetid  页面中需要widet_id来筛选
        * */
        *addTimeline({ payload: timeline }, { put, call, select }) {
            const { data } = yield call(create, timeline.timeline, timeline.widget_id);
            data.widget_id = timeline.widget_id;
            data.page_id = timeline.page_id;
            yield put({
                type: 'addTimelinesOfPage',
                payload: data,
            });
            yield put({
                type: 'editor/setState',
                payload: {
                    selectedTimelineId: data.id,
                    selectedTimeline: data,
                },
            });
        },
    },
    subscriptions: {

    },
};
